/*
 * KP ebdesk
 */

package com.ebdesk.controller.masterdata;

import com.ebdesk.model.masterdata.TableUserModel;
import com.ebdesk.model.masterdata.User;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Zulfikar <zulfikarfull@gmail.com>
 */
public class UserController {
    private UserManagement userManagement;
    private String[] columnNames = new String[]{"Username", "Name", "Email", "User Type", "Created By"};
    private int totalRow;
    
    public UserController() {
        try {
            userManagement = new UserManagement();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public TableUserModel getTableModelAllUser(int pageNumber, int rowsPerPage) throws SQLException {
        return getTableModel(getUserManagement().getAllUser(pageNumber, rowsPerPage));
    }
    
    public TableUserModel getTableModelSearchingUserByUsername(String username, int pageNumber, int rowsPerPage) throws SQLException {
        return getTableModel(getUserManagement().SearchingUserByUsername(username, pageNumber, rowsPerPage)); 
    }
    
    public TableUserModel getTableModelSearchingUserByName(String name, int pageNumber, int rowsPerPage) throws SQLException {
        return getTableModel(getUserManagement().SearchingUserByName(name, pageNumber, rowsPerPage));
    }
    
    public TableUserModel getTableModelSearchingUserByEmail(String email, int pageNumber, int rowsPerPage) throws SQLException {
        return getTableModel(getUserManagement().SearchingUserByEmail(email, pageNumber, rowsPerPage));
    }
    
    private TableUserModel getTableModel(List<User> dataUser) {
        Object[][] rowData = new Object[dataUser.size()][5];
        
        for (int i = 0; i < dataUser.size(); i++) {
            rowData[i][0] = dataUser.get(i).getUsername();
            rowData[i][1] = dataUser.get(i).getName();
            rowData[i][2] = dataUser.get(i).getEmail();
            rowData[i][3] = dataUser.get(i).getUser_type() == 1 ? "Admin" : "Non Admin";
            rowData[i][4] = dataUser.get(i).getCreated_by();
        }
        
        
        return new TableUserModel(rowData, columnNames);
    }

    /**
     * @return the userManagement
     */
    public UserManagement getUserManagement() {
        return userManagement;
    }

    /**
     * @return the totalRow
     */
    public int getTotalRow() {
        return this.getUserManagement().getTotalRow();
    }
    
  
    
    
    
    

}
